package pro.cherish.smartfile.rest.websocket;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pro.cherish.smartfile.config.websocket.WebSocketSender;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lys
 */
@RestController
@RequestMapping("/api/v1/websocket")
@Api(value = "websocket消息发送")
public class MsgController {
    @Autowired
    private ConcurrentHashMap<String, WebSocketSender> senderMap;

    @RequestMapping("/send")
    @ApiOperation("消息发送")
    public String sendMessage(@ApiParam("socket sender 查询key") @RequestParam String id, @ApiParam("消息内容") @RequestParam String content) {
        WebSocketSender sender = senderMap.get(id);
        if (sender != null) {
            sender.sendData(content);
            return String.format("Message '%s' sent to connection: %s.", content, id);
        } else {
            return String.format("Connection of id '%s' doesn't exist", id);
        }
    }
}